/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.kmagsoft.jconvert.gui;

import static com.kmagsoft.jconvert.gui.ApplicationParams.IMAGE_SEARCH_MAGICK_PATH;
import com.kmagsoft.jconvert.utils.GuiUtils;
import com.kmagsoft.jconvert.utils.PropUtils;
import gr.anagnosg.general.utils.*;
import gr.anagnosg.image.imagemagick.Constants;
import gr.anagnosg.image.imagemagick.ImageMagickUtils;
import gr.anagnosg.image.imagemagick.Param;
import gr.anagnosg.image.imagemagick.ParamParser;
import gr.anagnosg.image.imagemagick.ParameterList;
import gr.anagnosg.image.imagemagick.ParseParamsException;
import java.awt.Desktop;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;
import org.apache.tools.ant.DirectoryScanner;
/**
 *
 * @author anagnosg
 */
public class BatchConvertGui extends javax.swing.JFrame {
    public static int convertCount = 0;
    /**
     * Creates new form BatchConvertGui
     */
    public BatchConvertGui() throws FileNotFoundException {
        ApplicationParams.initBatchConvertParams();
        initComponents();
        extraInitComponents();
        afterCompsBatch();
    }
    
    
    public void loadActions(){
        this.actionSelect.removeAllItems();
        List<String> actions = PropUtils.loadParamActionValues();
        for(int i = 0 ; i < actions.size() ; i++){
            this.actionSelect.addItem(actions.get(i));
        }
    } 
    private void extraInitComponents(){
        //textArea.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);  
        //textArea.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);  
        //this.sourcesJTextField.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
       
        //GuiUtils.loadSelectListAppFile(actionSelect, null) ;
        loadActions();
        //Add global model to list. 
        this.jAddedOptionsList.setModel(jAddedOptionsListModel);
        try{
                
                if(Desktop.isDesktopSupported()){
                    //Desktop.getDesktop().browse(new URI("http://www.kmagsoft.com"));http://www.kmagsoft.com/index.php/jconvert
                    Desktop.getDesktop().browse(new URI("http://www.kmagsoft.com/index.php/jconvert"));//http://www.kmagsoft.com/index.php/jconvert
                }
              
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
        //dialog mini help 
        this.sourceTabKey =new KeyFileSelect(this.sourcesJTextField);
        this.outputTabKey = new KeyFileSelect(this.outputTextField);
    }
    public  void afterCompsBatch(){
        //LOAD previous values.
        //load data.
        File props = new File(ApplicationParams.INSTALLATION_PATH+File.pathSeparatorChar+"db.properties");
        //load properties.
        if(props.exists()){
            Properties prop = PropertiesUtils.loadProperties(props.getAbsolutePath(),false);
            //source dir.
            String sourcePath = prop.getProperty("sourceDir");
            if(sourcePath!=null && !sourcePath.equals("")){
                this.sourcesJTextField.setText(sourcePath);
                refreshSourceList();
            }            
            //dest dir 
            String destPath = prop.getProperty("destDir");
            if(destPath!=null ){
                this.outputTextField.setText(destPath);
            }
            
            
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane5 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jpanelReportArea = new javax.swing.JScrollPane();
        reportArea = new javax.swing.JTextArea();
        outputTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        outputList = new javax.swing.JList();
        jButton2 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        sourceSelected = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        sourceTotal = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        sourceList = new javax.swing.JList();
        jButton7 = new javax.swing.JButton();
        jSourceRefresh = new javax.swing.JButton();
        sourcesJTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        outputTotalLab = new javax.swing.JLabel();
        sourceBrowseBut = new javax.swing.JButton();
        removeAllBut = new javax.swing.JButton();
        addBut = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        convert = new javax.swing.JButton();
        removeBut = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane6 = new javax.swing.JScrollPane();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        actionSelect = new javax.swing.JComboBox();
        jParamSelect = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jInputParam = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        AddActionBut = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jpreviewCommand = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        jAddedOptionsList = new javax.swing.JList();
        jRemoveAddedOptionBut = new javax.swing.JButton();
        jRemoveAllAddedOptionBut = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Batch Convert v1.4");
        setPreferredSize(new java.awt.Dimension(989, 791));

        jScrollPane5.setBorder(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jPanel1.setPreferredSize(new java.awt.Dimension(800, 600));

        jLabel10.setText("Report:");

        jpanelReportArea.setMinimumSize(new java.awt.Dimension(242, 100));
        jpanelReportArea.setPreferredSize(new java.awt.Dimension(242, 100));

        reportArea.setColumns(20);
        reportArea.setRows(5);
        reportArea.setMinimumSize(new java.awt.Dimension(0, 100));
        reportArea.setPreferredSize(new java.awt.Dimension(240, 100));
        jpanelReportArea.setViewportView(reportArea);

        outputTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                outputTextFieldFocusLost(evt);
            }
        });
        outputTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                outputTextFieldKeyPressed(evt);
            }
        });

        jButton1.setText("Open Output");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton6.setText("Select Folder");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jLabel4.setText("Output Folder: ");

        jButton4.setText("Save List");

        jButton5.setText("Load List");

        outputList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                outputListValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(outputList);

        jButton2.setText("Stop");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel9.setText("Total:");

        sourceSelected.setText("0");

        jLabel7.setText("Selected:");

        sourceTotal.setText("0");

        jLabel8.setText("Total:");

        sourceList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                sourceListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(sourceList);

        jButton7.setText("Help");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jSourceRefresh.setText("Refresh");
        jSourceRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSourceRefreshActionPerformed(evt);
            }
        });

        sourcesJTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sourcesJTextFieldActionPerformed(evt);
            }
        });
        sourcesJTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                sourcesJTextFieldFocusLost(evt);
            }
        });
        sourcesJTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                sourcesJTextFieldKeyTyped(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                sourcesJTextFieldKeyPressed(evt);
            }
        });

        jLabel1.setText("Input Folder");

        jLabel2.setText("FIles:");

        outputTotalLab.setText("0");

        sourceBrowseBut.setText("Select Folder");
        sourceBrowseBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sourceBrowseButActionPerformed(evt);
            }
        });

        removeAllBut.setText("Remove All");
        removeAllBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeAllButActionPerformed(evt);
            }
        });

        addBut.setText("Add");
        addBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButActionPerformed(evt);
            }
        });

        jLabel3.setText("Selected Files:");

        convert.setText("Convert");
        convert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                convertActionPerformed(evt);
            }
        });

        removeBut.setText("Remove");
        removeBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButActionPerformed(evt);
            }
        });

        jButton9.setText("Add All");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jTabbedPane1.setBorder(null);

        jPanel2.setBorder(null);
        jPanel2.setPreferredSize(new java.awt.Dimension(800, 232));

        jLabel5.setText("Action");

        jLabel13.setText("Parameter");

        actionSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actionSelectActionPerformed(evt);
            }
        });

        jParamSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jParamSelectActionPerformed(evt);
            }
        });

        jLabel6.setText("OR");

        jInputParam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jInputParamActionPerformed(evt);
            }
        });

        jButton3.setText("Configure");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        AddActionBut.setText("Add");
        AddActionBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddActionButActionPerformed(evt);
            }
        });

        jLabel12.setText("Preview Command  ");

        jpreviewCommand.setColumns(20);
        jpreviewCommand.setRows(5);
        jScrollPane4.setViewportView(jpreviewCommand);

        jAddedOptionsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jScrollPane3.setViewportView(jAddedOptionsList);

        jRemoveAddedOptionBut.setText("Remove Option");
        jRemoveAddedOptionBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRemoveAddedOptionButActionPerformed(evt);
            }
        });

        jRemoveAllAddedOptionBut.setText("Remove All");
        jRemoveAllAddedOptionBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRemoveAllAddedOptionButActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel12)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 447, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(19, 19, 19)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRemoveAddedOptionBut)
                            .addComponent(jRemoveAllAddedOptionBut)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(actionSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel13)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jParamSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jButton3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(AddActionBut))
                                    .addComponent(jInputParam, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap(72, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jRemoveAddedOptionBut, jRemoveAllAddedOptionBut});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(actionSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jParamSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jInputParam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(jButton3)
                            .addComponent(AddActionBut))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(jRemoveAddedOptionBut)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jRemoveAllAddedOptionBut))
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        jScrollPane6.setViewportView(jPanel2);

        jTabbedPane1.addTab("Image", jScrollPane6);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jSourceRefresh))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(sourcesJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(sourceBrowseBut))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(sourceSelected, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel9)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(sourceTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 435, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(convert, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(addBut, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(removeBut, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(removeAllBut)
                                    .addComponent(jButton2))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(outputTotalLab))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
                            .addComponent(outputTextField, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButton4, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jButton5))
                                .addComponent(jButton6))
                            .addComponent(jButton7))
                        .addGap(21, 21, 21))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jpanelReportArea, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jTabbedPane1))))
                .addGap(226, 226, 226))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addBut, convert, jButton2, jButton9, removeAllBut, removeBut});

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton4, jButton5, jButton6});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jSourceRefresh)
                            .addComponent(jLabel4)
                            .addComponent(jButton1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(sourcesJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(sourceBrowseBut))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel7)
                                    .addComponent(sourceSelected)
                                    .addComponent(jLabel9)
                                    .addComponent(sourceTotal))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(convert)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(addBut)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(removeBut)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(removeAllBut)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(outputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel8)
                            .addComponent(outputTotalLab))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jButton4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton5))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpanelReportArea, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        jScrollPane5.setViewportView(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 989, Short.MAX_VALUE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 762, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here:

        ListModel model = this.sourceList.getModel();
        int[] selectedIndeces = this.sourceList.getSelectedIndices();
        //String values[] = new String[selectedIndex.length];
        DefaultListModel outputListModel = new DefaultListModel();
        for(int i = 0 ; i < model.getSize();i++){
            String s = (String)model.getElementAt(i);

            outputListModel.addElement(s);
        }
        this.outputList.setModel(outputListModel);
        this.outputTotalLab.setText(""+this.outputList.getModel().getSize());
    }//GEN-LAST:event_jButton9ActionPerformed

    private void removeButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButActionPerformed
        // TODO add your handling code here:
        DefaultListModel model = (DefaultListModel) this.outputList.getModel();
        int[] selectedIndeces = this.outputList.getSelectedIndices();
        //String values[] = new String[selectedIndex.length];
        //DefaultListModel outputListModel = new DefaultListModel();
        for(int i = 0 ; i < selectedIndeces.length;i++){
            int indexT = selectedIndeces[i];
            //model.getElementAt(i).
            String vT = (String)model.getElementAt(indexT);
            model.remove(indexT);
        }
        this.outputList.setModel(model);
        this.outputTotalLab.setText(""+this.outputList.getModel().getSize());
    }//GEN-LAST:event_removeButActionPerformed

    private void convertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertActionPerformed
        // TODO add your handling code here:
        //TODO Open default Browser
        //Open Report Dialog.
        //final ReportDialog repD= new ReportDialog(this,true);
        //convert images.
        boolean runconvert=false;
        runProcess = true;
        if(outputList!=null &&outputList.getModel().getSize()>0){
            runconvert=true;
        }
        else{
            JOptionPane.showMessageDialog(this, "Please add some files to be processed."
                ,"Warning", JOptionPane.PLAIN_MESSAGE);
        }
        //params
        if(params.getParams().size()==0){
            JOptionPane.showMessageDialog(this, "Please add some actions to be performed."
                ,"Warning", JOptionPane.PLAIN_MESSAGE);
            runconvert=false;
        }
        if(runconvert){
            Thread t = new Thread(){

                public void run(){
                    double totalSizeBefore=0;
                    double totalSizeAfter =0;
                    long timeStart = new Date().getTime();
                    String[] outputFiles = new String[outputList.getModel().getSize()];
                    Param mergeParam = params.hasAction(Constants.MERGE_ACTION);
                    boolean hasMerge = false;
                    List<String> mergedFilenames=new ArrayList<String>();
                    String mergedFilenamesStr ="";

                    List<String> inputmergedFilenames=new ArrayList<String>();
                    String inputmergedFilenamesStr ="";

                    boolean processed = false;
                    if(mergeParam!=null ){
                        hasMerge  = true;
                    }
                    String parentSource = sourcesJTextField.getText();
                    String parentDest = outputTextField.getText();
                    for(int i = 0 ; i<outputList.getModel().getSize();i++){
                        //String reportMessage="";
                        if(runProcess) {
                            String vT = (String)outputList.getModel().getElementAt(i);

                            final String inputfile = parentSource+File.separatorChar+vT;
                            String renamedFile = vT;//TODO apply rename rules.

                            Param paramChange =  params.hasChangeFormat();
                            if(paramChange!=null){
                                //paramChange.get
                                String toChangeExt = paramChange.getValue().trim();
                                renamedFile = FileUtils.changeExtension(renamedFile, toChangeExt);
                            }

                            String outputfile = parentDest+File.separatorChar+renamedFile;
                            double sizeBefore=(double)new File(inputfile).length()/(double)(1024*1024);
                            totalSizeBefore+=sizeBefore;
                            if(hasMerge){

                                mergedFilenames.add(outputfile);// +=" "+outputfile;
                                mergedFilenamesStr+=" "+outputfile;
                                inputmergedFilenames.add(inputfile);
                                inputmergedFilenamesStr+=" "+inputfile;
                            }

                            //this.reportArea.append("Processing "+(i+1)+" image of "+this.outputList.getModel().getSize()+", input file "+inputfile+" output "+outputfile+".");
                            try {
                                String convertCommand = getConvertCommand();//ApplicationParams.IMAGE_SEARCH_MAGICK_PATH+File.separatorChar+"convert ";
                                //executeCommand(String command)
                                List<String> commandList = params.commandStringList(convertCommand, inputfile, outputfile); //params.commandString(convertCommand,inputfile, outputfile);
                                String command = params.commandString(convertCommand, outputfile, outputfile);
                                String output ="";
                                if(!command.equals("")){
                                    processed=true;
                                    updateGUI("Processing "+(i+1)+" image of "+outputList.getModel().getSize()+", input file "+vT+" output "+outputfile+".\n");
                                    output = ExtrernalUtils.executeCommandBuilder(commandList);
                                    updateGUI(command +"\n"+output);
                                    double sizeAfter =(double)new File(outputfile).length()/(double)(1024*1024);
                                    //System.out.println("sizeAfter"+sizeAfter);
                                    totalSizeAfter+=sizeAfter;
                                    outputFiles[i]=outputfile;
                                    //this.reportArea.append("Processing Finished. Size before "+sizeBefore+"MB , after "+sizeAfter+"MB \n");
                                    updateGUI("Processing Finished. Size before "+MathUtils.round(sizeBefore)+"MB , after "+MathUtils.round(sizeAfter)+"MB \n");
                                }

                                //ImageMagickUtils.resizeSingle(800, 600, inputfile, outputfile);

                            } catch (Exception ex){Logger.getLogger(BatchConvertGui.class.getName()).log(Level.SEVERE, null, ex);}

                        }
                        else{
                            updateGUI("Convert was stopped\n");
                            break;
                        }
                    }

                    //mergeFilenames
                    if(hasMerge){
                        String str=mergedFilenamesStr;
                        try{
                            String convertCommand = getConvertCommand();
                            String output ="";
                            if(processed){

                                /*mergedFilenames.add(0, convertCommand);
                                mergedFilenames.add(mergedFilenames.size(), parentDest+File.separatorChar+mergeParam.getParamOption());
                                output = ExtrernalUtils.executeCommandBuilder(mergedFilenames);*/
                                output=GuiUtils.splitCommand( parentDest,  parentDest+File.separatorChar+mergeParam.getParamOption(), convertCommand, mergedFilenames,null);
                                updateGUI(convertCommand +"\n"+str +" "+parentDest+File.separatorChar+mergeParam.getParamOption()+"\n output: "+output);
                            }
                            else{
                                str=inputmergedFilenamesStr;
                                /*inputmergedFilenames.add(0, convertCommand);
                                inputmergedFilenames.add(mergedFilenames.size(), parentDest+File.separatorChar+mergeParam.getParamOption());
                                output = ExtrernalUtils.executeCommandBuilder(inputmergedFilenames);*/
                                output =GuiUtils.splitCommand( parentDest,  parentDest+File.separatorChar+mergeParam.getParamOption(), convertCommand, inputmergedFilenames,null);
                                updateGUI(convertCommand +"\n"+str +" "+parentDest+File.separatorChar+mergeParam.getParamOption()+"\n output: "+output);

                            }

                        }
                        catch(Exception ex){
                            ex.printStackTrace();
                        }
                    }
                    long endTime = new Date().getTime();
                    long diffTime = (endTime-timeStart)/1000;
                    //this.reportArea.append("All files has been processed. Total size before "+totalSizeBefore+"MB after"+totalSizeAfter+"MB.\n" );
                    //this.reportArea.append("Time ellapsed "+diffTime+" sec.\n" );
                    //
                    //totalSizeAfter = (double)FileUtils.calcTotalSize(outputFiles)/(double)(1024*1024);
                    totalSizeAfter=MathUtils.round(totalSizeAfter);//  round(totalSizeAfter,2);
                    totalSizeBefore=MathUtils.round(totalSizeBefore);
                    double ration= totalSizeBefore/totalSizeAfter;
                    ration=MathUtils.round(ration);
                    updateGUI("All files has been processed. Total size before "+totalSizeBefore+"MB after"+totalSizeAfter+"MB. Ratio "+ration+" \n");
                    updateGUI("Time ellapsed "+diffTime+" sec.\n");
                }
                public void updateGUI(final String message){
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            // Here, we can safely update the GUI
                            // because we'll be called from the
                            // event dispatch thread
                            reportArea.append(message);
                        }
                    });
                }
            };
            t.start();

        }
        else{
            reportArea.append("No Images Selected!");
        }
    }//GEN-LAST:event_convertActionPerformed

    private void addButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButActionPerformed
        // TODO add your handling code here:
        ListModel model = this.sourceList.getModel();
        int[] selectedIndeces = this.sourceList.getSelectedIndices();
        //String values[] = new String[selectedIndex.length];
        ListModel outputListModel = (ListModel)this.outputList.getModel();
        DefaultListModel newList = new DefaultListModel();
        for(int i = 0 ; i <outputListModel.getSize();i++ ){
            String vT = (String)outputListModel.getElementAt(i);
            newList.addElement(vT);

        }
        for(int i = 0 ; i < selectedIndeces.length;i++){
            int indexT = selectedIndeces[i];
            String vT = (String)model.getElementAt(indexT);
            if(!newList.contains(vT)){
                newList.addElement(vT);
            }
        }
        this.outputList.setModel(newList);
        this.outputTotalLab.setText(""+this.outputList.getModel().getSize());
    }//GEN-LAST:event_addButActionPerformed

    private void removeAllButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeAllButActionPerformed
        // TODO add your handling code here:

        DefaultListModel model = (DefaultListModel) this.outputList.getModel();
        model.clear();
        this.outputList.setModel(model);
        this.outputTotalLab.setText(""+this.outputList.getModel().getSize());
    }//GEN-LAST:event_removeAllButActionPerformed

    private void sourceBrowseButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sourceBrowseButActionPerformed
        // TODO add your handling code here:

        JFileChooser chooser = new JFileChooser();
        File sourceDirF = null;
        if(this.isDir(this.sourcesJTextField)){
            sourceDirF= new File(this.sourcesJTextField.getText());

        }
        chooser.setCurrentDirectory(sourceDirF);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int option = chooser.showOpenDialog(this);
        if (option == JFileChooser.APPROVE_OPTION) {
            /*statusbar.setText("You opened " + ((chooser.getSelectedFile()!=null)?
                chooser.getSelectedFile().getName():"nothing"));*/

        //sourcesJTextField
        String parentDir = chooser.getSelectedFile().getAbsolutePath();
        this.sourcesJTextField.setText(parentDir);

        //refresh list.
        /*this.sourceList.setModel(new DefaultListModel());
        File sourceParent = new File(parentDir);
        File[] sourceFilesT = sourceParent.listFiles();
        DefaultListModel model = new DefaultListModel();
        for(int i = 0 ; i < sourceFilesT.length ; i ++ ){
            String fileTStr = sourceFilesT[i].getName(); //getAbsolutePath();
            model.addElement(fileTStr);
        }
        this.sourceList.setModel(model);
        this.sourceTotal.setText(""+model.size());
        */
        refreshSourceList();
        PropUtils.saveProperty("sourceDir",parentDir);
        }
        else {
            //statusbar.setText("You canceled.");
        }

        //save to db for next use.
    }//GEN-LAST:event_sourceBrowseButActionPerformed

    private void sourcesJTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sourcesJTextFieldKeyPressed
        // TODO add your handling code here:

        sourceTabKey.keyPressed(evt);

    }//GEN-LAST:event_sourcesJTextFieldKeyPressed

    private void sourcesJTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sourcesJTextFieldKeyTyped
        // TODO add your handling code here:
        if(evt.getKeyChar()!=KeyEvent.VK_TAB){
            refreshSourceListWithWildChars();
        }

    }//GEN-LAST:event_sourcesJTextFieldKeyTyped

    private void sourcesJTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_sourcesJTextFieldFocusLost
        // TODO add your handling code here:
        //evt.getSource()
        this.sourceTabKey.resetSelect();
    }//GEN-LAST:event_sourcesJTextFieldFocusLost

    private void sourcesJTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sourcesJTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_sourcesJTextFieldActionPerformed

    private void jSourceRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSourceRefreshActionPerformed
        // TODO add your handling code here:

        String parentDir = this.sourcesJTextField.getText();
        refreshSourceList();
        PropUtils.saveProperty("sourceDir",parentDir);

    }//GEN-LAST:event_jSourceRefreshActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:

        ParametersDialog parD = new ParametersDialog(this,true);
        parD.setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:

        JOptionPane.showMessageDialog(this, "Select input folder, where are the images that you want to convert.\n"
            + "Select and add images that you want to process.\nSelect an output folder where the processed images will be stored.\n"
            + "Select the actions that you want to perform and add them.\nPress convert to start. If you want to stop press stop.\n"
            + "Email me anagnosg@yahoo.gr"
            ,"Help", JOptionPane.PLAIN_MESSAGE);
    }//GEN-LAST:event_jButton7ActionPerformed

    private void sourceListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_sourceListValueChanged
        // TODO add your handling code here:

        this.sourceSelected.setText(""+this.sourceList.getSelectedIndices().length);
    }//GEN-LAST:event_sourceListValueChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:

        this.runProcess = false;
    }//GEN-LAST:event_jButton2ActionPerformed

    private void outputListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_outputListValueChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_outputListValueChanged

    private void jParamSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jParamSelectActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jParamSelectActionPerformed

    private void AddActionButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddActionButActionPerformed
        // TODO add your handling code here:

        //
        String parameters ="";
        //TODO logic to implement parameters according to user selects.
        if(this.jInputParam.getText()!=null && !this.jInputParam.getText().equals("")){
            parameters = this.jInputParam.getText();

        }
        else{
            parameters = this.jParamSelect.getSelectedItem().toString();
        }
        try{
            //Param p= ParamParser.parseParameters( parameters, this.actionSelect.getSelectedItem().toString() );
            Param p= new Param();//ParamParser.parseParameters( parameters, this.actionSelect.getSelectedItem().toString() );
            p.setType(this.actionSelect.getSelectedItem().toString());
            if(!p.getType().equals(Constants.CHANGE_FORMAT_STR)){
                p.setAction("-"+this.actionSelect.getSelectedItem().toString());
                p.setParamOption(parameters);
                p.setValue("-"+this.actionSelect.getSelectedItem().toString()+" "+parameters);
            }
            else {
                p.setAction("-"+this.actionSelect.getSelectedItem().toString());
                p.setParamOption(parameters);
                p.setValue(""+parameters);
            }
            this.params.add(p);
            String filenameSource = "sourceFile";
            String convertCommand = getConvertCommand() ;//ApplicationParams.IMAGE_SEARCH_MAGICK_PATH+File.separatorChar+"convert ";
            String filenameDest = "destFile";
            String currentCommand = this.params.commandString(convertCommand,filenameSource, filenameDest);
            this.jpreviewCommand.setText(currentCommand);
            this.jAddedOptionsListModel.addElement(p.getValue());
            //this.jAddedOptionsList.add

        }
        catch(Exception parex){
            parex.printStackTrace();

        }
    }//GEN-LAST:event_AddActionButActionPerformed

    private void actionSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionSelectActionPerformed
        // TODO add your handling code here:
        //on select value fill parameters options from application properties file.

        String selectedAction  = (String)this.actionSelect.getModel().getSelectedItem();
        if(selectedAction!=null && !selectedAction.equals("")){
            //read application properties files and fill
            GuiUtils.loadSelectListAppFile(this.jParamSelect,selectedAction);
        }

    }//GEN-LAST:event_actionSelectActionPerformed

    private void jInputParamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jInputParamActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jInputParamActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:

        JFileChooser chooser = new JFileChooser();
        File sourceDirF = null;
        if(this.isDir(this.outputTextField)){
            sourceDirF= new File(this.outputTextField.getText());

        }
        chooser.setCurrentDirectory(sourceDirF);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int option = chooser.showOpenDialog(this);
        if (option == JFileChooser.APPROVE_OPTION) {
            /*statusbar.setText("You opened " + ((chooser.getSelectedFile()!=null)?
                chooser.getSelectedFile().getName():"nothing"));*/

        //sourcesJTextField
        String parentDirOutput = chooser.getSelectedFile().getAbsolutePath();
        this.outputTextField.setText(parentDirOutput);
        PropUtils.saveProperty("destDir",parentDirOutput);

        }
        else {
            //statusbar.setText("You canceled.");
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        ExtrernalUtils.openDirWithDefaultOSFileExplorer(this.outputTextField.getText());
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jRemoveAllAddedOptionButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRemoveAllAddedOptionButActionPerformed
        // TO FolderDO add your handling code here:
        this.jAddedOptionsListModel.removeAllElements();
        this.params.setParams(new ArrayList());
        String filenameSource = "sourceFile";
        String filenameDest = "destFile";
        String convertCommand = getConvertCommand();//ApplicationParams.IMAGE_SEARCH_MAGICK_PATH+File.separatorChar+"convert ";
        String currentCommand = this.params.commandString(convertCommand,filenameSource, filenameDest);
        this.jpreviewCommand.setText(currentCommand);
    }//GEN-LAST:event_jRemoveAllAddedOptionButActionPerformed

    private void jRemoveAddedOptionButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRemoveAddedOptionButActionPerformed
        // TODO add your handling code here:
        int selIndex = this.jAddedOptionsList.getSelectedIndex();
        if(selIndex>-1){
            this.jAddedOptionsListModel.remove(selIndex);
            //this.jAddedOptionsList.setModel(jAddedOptionsListModel);
            this.params.getParams().remove(selIndex);
            String filenameSource = "sourceFile";
            String filenameDest = "destFile";
            String convertCommand = getConvertCommand();//ApplicationParams.IMAGE_SEARCH_MAGICK_PATH+File.separatorChar+"convert ";
            String currentCommand = this.params.commandString(convertCommand,filenameSource, filenameDest);
            this.jpreviewCommand.setText(currentCommand);
        }
    }//GEN-LAST:event_jRemoveAddedOptionButActionPerformed

    private void outputTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_outputTextFieldKeyPressed
        // TODO add your handling code here:

        this.outputTabKey.keyPressed(evt);
    }//GEN-LAST:event_outputTextFieldKeyPressed

    private void outputTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_outputTextFieldFocusLost
        // TODO add your handling code here:

        this.outputTabKey.resetSelect();
    }//GEN-LAST:event_outputTextFieldFocusLost

    private boolean isDir(JTextField tf){
        String v = tf.getText();
        File ft = new File(v);
        if(ft.exists()&& ft.isDirectory()){
            return true;
        }
        return false;
    }    
   
    
    
    
    
    private void refreshReportArea(final JTextArea textArea, final String text){
       
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              textArea.append(text);
            }
        });
    }    
    
    private void refreshSourceList(){
            this.sourceList.setModel(new DefaultListModel());
            File sourceParent = new File(this.sourcesJTextField.getText());
            File[] sourceFilesT = sourceParent.listFiles();
            DefaultListModel model = new DefaultListModel();
            if(sourceFilesT!=null){
                for(int i = 0 ; i < sourceFilesT.length ; i ++ ){
                    String fileTStr = sourceFilesT[i].getName(); //getAbsolutePath();
                    model.addElement(fileTStr);
                }
            }
            this.sourceList.setModel(model);
            this.sourceTotal.setText(""+model.size());
    }
    
    private String[] findMatchingDirs(){
        String parent2Search = this.sourcesJTextField.getText();
        return FileUtils.findMatchingDirs(parent2Search);
    }
    
    private void refreshSourceListWithWildChars(){
            String parent2Search = this.sourcesJTextField.getText();
            
            File fp = new File(parent2Search);
            boolean isdir = fp.exists() && fp.isDirectory();
            if(parent2Search.endsWith(""+File.separatorChar) || isdir){//TODO IF IS DIR SAME FUNCTION.
                refreshSourceList();
            }
            else{
                this.sourceList.setModel(new DefaultListModel());
                String[] files = findMatchingDirs();
                DefaultListModel model = new DefaultListModel();
                if(files!=null){
                    for(int i = 0 ; i < files.length ; i ++ ){
                        String fileTStr = files[i]; //getAbsolutePath();
                        model.addElement(fileTStr);
                    }
                }
                this.sourceList.setModel(model);
                this.sourceTotal.setText(""+model.size());
            }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BatchConvertGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BatchConvertGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BatchConvertGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BatchConvertGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new BatchConvertGui().setVisible(true);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(BatchConvertGui.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddActionBut;
    private javax.swing.JComboBox actionSelect;
    private javax.swing.JButton addBut;
    private javax.swing.JButton convert;
    private javax.swing.JList jAddedOptionsList;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton9;
    private javax.swing.JTextField jInputParam;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JComboBox jParamSelect;
    private javax.swing.JButton jRemoveAddedOptionBut;
    private javax.swing.JButton jRemoveAllAddedOptionBut;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JButton jSourceRefresh;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JScrollPane jpanelReportArea;
    private javax.swing.JTextArea jpreviewCommand;
    private javax.swing.JList outputList;
    private javax.swing.JTextField outputTextField;
    private javax.swing.JLabel outputTotalLab;
    private javax.swing.JButton removeAllBut;
    private javax.swing.JButton removeBut;
    private javax.swing.JTextArea reportArea;
    private javax.swing.JButton sourceBrowseBut;
    private javax.swing.JList sourceList;
    private javax.swing.JLabel sourceSelected;
    private javax.swing.JLabel sourceTotal;
    private javax.swing.JTextField sourcesJTextField;
    // End of variables declaration//GEN-END:variables
    private ParameterList params  = new ParameterList(); 
    private DefaultListModel jAddedOptionsListModel = new  DefaultListModel();
    
    private boolean runProcess = true;
    private KeyFileSelect sourceTabKey;
    private KeyFileSelect outputTabKey;
    
    public static String getConvertCommand(){
        return  ""+ApplicationParams.IMAGE_SEARCH_MAGICK_PATH+File.separatorChar+"convert"; 
    }
    
    public static String getMogrifyCommand(){
        return  ""+ApplicationParams.IMAGE_SEARCH_MAGICK_PATH+File.separatorChar+"mogrify"; 
    }
}


